<?php
/*
 * report controller 2012.8.16 creport.php
 */
class creport extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->model ( 'm_report', '', TRUE );
		session_start();
		// load taoke api lib
		$this->load->library ( array ('pagination','table','commonClass','RequestCheckUtil','Report','TopClient') );
	}
		
	public function index() {
		$ip = $this->input->ip_address ();
		/*if(!in_array($ip, array('61.174.30.170','125.116.233.224','172.21.15.108', '127.0.0.1'))){
			$url = '/';
			header('Location: ' .site_url());
			exit;
		}*/
		$res = $this->m_report->view_report();
		$c = new TopClient ();
		$res = $c->get_object_vars_final($res);
		$total = 0;
		$result['data'] = "<table><thead><tr><th>" . $this->_lang['r_trade_id'] . "</th><th>" . $this->_lang ['r_item_title'] . "</th><th>" . $this->_lang ['r_pay_price'] . "</th><th>" . $this->_lang ['r_commission'] . "</th><th>" . $this->_lang ['r_report_date'] . "</th><th>" . $this->_lang ['r_is_fanli'] . "</th><th>" . $this->_lang ['r_fanli_date'] . "</th><th>" . $this->_lang ['r_fanli_price'] . "</th><th>" . $this->_lang ['r_total'] . "</th></tr></thead>";
		foreach ( $res as $key ) {
			$total = $total + floatval($key ['commission']);
			$result['data'] .= "<tbody id='list_data'><tr>";
			$result['data'] .= "<td>" . $key ['trade_id'] . "</td>";
			$result['data'] .= "<td>" . $key ['item_title'] . "</td>";
			$result['data'] .= "<td>" . $key ['real_pay_fee'] . "</td>";
			$result['data'] .= "<td>" . $key ['commission'] . "</td>";
			$result['data'] .= "<td>" . $key ['report_date'] . "</td>";
			$result['data'] .= "<td>" . $key ['is_fanli'] . " </td>";
			$result['data'] .= "<td>" . date("Y-m-d",$key ['fanli_time']) . "</td>";
			$result['data'] .= "<td>" . $key ['fanli_money'] . "</td>";
			$result['data'] .= "<td>" . $total . "</td></tr></tbody>";
		}
		$result['data'] .= "</table>";
		
		$this->load->view('v_report',$result);
	}
	
	/*
	 * the main function get report
	 */
	public function get_report() {
		$bb = $this->input->post ('date');
		$date = date("Ymd",strtotime($bb));
		if($this->m_report->query_report($date)){
			$aa = $this->act_get_report($date);
			$res['msg'] = "GET[".$aa."]RECORD";
			echo json_encode($res);exit();
		}
		else{
			$res['msg'] = "already has";
			echo json_encode($res);exit();
		}
	}
	
	/*
	 * the main function to act get report function
	 */
	private function act_get_report($date='') {
		$c = new TopClient ();
		$c->appkey = APPKEY;
		$c->secretKey = SECRETKEY;
		$req = new Report ();
		$req->setFields ( "trade_parent_id,trade_id,real_pay_fee,commission_rate,commission,app_key,outer_code,pay_time,pay_price,num_iid,item_title,item_num,category_id,category_name,seller_nick,shop_title" );
		$req->setDate ( $date );
		$resp = $c->execute ( $req, SESSIONKEY );
		$res = $c->get_object_vars_final ( $resp );
		$val = @$res ['taobaoke_report'];
		$report = array ();
		if (@$val ['total_results'] == 0) {
			return false;
		} else {
			if ($val ['total_results'] > 1) {
				$rep = $report [0] = $val ['taobaoke_report_members'] ['taobaoke_report_member'];
				$report = array ();
				$i = 0;
				while ( $i < $val ['total_results'] ) {
					$report [$i] = $rep [$i];
					if(empty($report[$i]['category_name'])){
						$report[$i]['category_name'] = "nothing";
					}
					$report [$i] ['report_date'] = $date;
					$report [$i] ['add_time'] = time ();
					$i ++;
				}
				$this->m_report->insert_report ( $report );
			} else {
				$report = $val ['taobaoke_report_members'] ['taobaoke_report_member'];
				$report ['report_date'] = $date;
				$report ['add_time'] = time ();
				$this->m_report->insert_one_report ( $report );
			}
		}
		return $val['total_results'];
	}
}